package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalaryMonthEndSubmitDao;
import com.boss.salary.vo.SalaryMonthEndVO;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Repository
public class SalaryMonthEndSubmitDaoImpl extends BaseDaoImpl implements ISalaryMonthEndSubmitDao {
    @Override
    public void batchSaveSalaryMonthSubmit(List<SalaryMonthEndVO> salaryMonthEndVOList) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        if (salaryMonthEndVOList.size() > 0) {
            List<Object[]> paramList = new ArrayList<>(salaryMonthEndVOList.size());
            Class<?> clazz = SalaryMonthEndVO.class;
            for (SalaryMonthEndVO salaryMonthEndVO : salaryMonthEndVOList) {
                Object[] paramArr = {
                        SysUtils.generateGuid(), String.valueOf(DateUtils.getCurrentTimeStamp()), String.valueOf(DateUtils.getCurrentTimeStamp()),
                        salaryMonthEndVO.getAgencyId(), salaryMonthEndVO.getPersonId(), salaryMonthEndVO.getMofDivCode(), salaryMonthEndVO.getIsThirteen()
                };
                List<Object> params = new ArrayList<>(Arrays.asList(paramArr));
                for (int i = 1; i < 101; i++) {
                    Method getMethod = clazz.getDeclaredMethod("getS" + i);
                    params.add(getMethod.invoke(salaryMonthEndVO));
                }
                paramList.add(params.toArray());
            }
            StringBuilder sb = new StringBuilder();
            sb.append(" insert into ").append(TableName.SALARY_MONTH_END_SUBMIT)
                    .append(" ( id, create_time, update_time, agency_id, person_id, mof_div_code,is_thirteen, ")
                    .append(" s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,  ")
                    .append(" s21, s22, s23, s24, s25, s26, s27, s28, s29, s30, s31, s32, s33, s34, s35, s36, s37, s38, s39, s40, ")
                    .append(" s41, s42, s43, s44, s45, s46, s47, s48, s49, s50, s51, s52, s53, s54, s55, s56, s57, s58, s59, s60, ")
                    .append(" s61, s62, s63, s64, s65, s66, s67, s68, s69, s70, s71, s72, s73, s74, s75, s76, s77, s78, s79, s80, ")
                    .append(" s81, s82, s83, s84, s85, s86, s87, s88, s89, s90, s91, s92, s93, s94, s95, s96, s97, s98, s99, s100 ) ")
                    .append(" values ( ?, ?, ?, ?, ?, ?,?, ")
                    .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                    .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                    .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                    .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                    .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ");
            super.getJdbcTemplate().batchUpdate(sb.toString(), new BatchPreparedStatementSetter() {
                public int getBatchSize() {
                    return paramList.size();
                    //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
                }
                public void setValues(PreparedStatement ps, int i)throws SQLException {
                    Object[] sme = paramList.get(i);
                    for(int a = 0; a < 107; a++){
                        ps.setObject(a+1,sme[a]);
                    }

                }
            });
        }
    }

    @Override
    public void batchPhysicalDeleteSalaryMonthSubmit(String province, List<String> agencyIds) {
        StringBuilder sb = new StringBuilder();
        sb.append(" delete from ").append(TableName.SALARY_MONTH_END_SUBMIT)
                .append(" where ").append(" mof_div_code = ? ");
        if(!CollectionUtils.isEmpty(agencyIds)){
            sb.append(" and ").append(SysUtils.inSql("agency_id", agencyIds));
        }
        super.getjdbcTemplate().update(sb.toString(), province);
    }
}
